<?php

function isEmpty($var){

return (isset($_POST['club']) && $_POST['club']!=""); 

}

function listQuery($tabRunners,$title){
	
	if($tabRunners!=0){
	
	require_once("bd_utils.php");
		$obj_utils=new bd_utils();		
		$obj_utils->connect();
		$time=$obj_utils->getTime();				
		$obj_utils->close();

	
	?>
	<table class="principale">
		<tr><th colspan="8"><h2><?php echo $title;?></h2></th></tr>
		<tr><th>Position</th><th>Nom</th><th>Prénom</th><th>Dossard</th><th>Temps</th><th>Féminin</th><th>T/Km</th><th>KM/H</th></tr>
	
	<?php 
	$cptF=1;
	for($i=0;$i<sizeof($tabRunners);$i++){?>
		
		<tr class="list">
		<td class="listing"><?php echo($i+1); ?></td>
			<td class="listing"><?php echo($tabRunners[$i]['nom']); ?></td>
			<td class="listing"><?php echo($tabRunners[$i]['prenom']); ?></td>
			<td class="listing" align="center"><?php echo($tabRunners[$i]['dossard']); ?></td>
			<td class="listing" align="center"><?php echo(getTimeDif($time,$tabRunners[$i]['temp'])); ?></td>
			<td class="listing" align="center"><?php if($tabRunners[$i]['sexe']=="F"){echo($cptF++);}?></td>
			<td class="listing" align="center"><?php echo(getMinOnKm(getTimeDif($time,$tabRunners[$i]['temp']),$tabRunners[$i]['distance'])); ?></td>
			<td class="listing" align="center"><?php echo(getKmH(getTimeDif($time,$tabRunners[$i]['temp']),$tabRunners[$i]['distance'])); ?></td>
			
		</tr>
	
	<?php } ?>
	</table>
<?php
	
	}

}

function listRunnersLeft($tabRunners,$title){
	
	if($tabRunners!=0){
	
	require_once("bd_utils.php");
		$obj_utils=new bd_utils();		
		$obj_utils->connect();
		$time=$obj_utils->getTime();				
		$obj_utils->close();

	
	?>
	<table class="principale">
		<tr><th colspan="8"><h2><?php echo $title;?></h2></th></tr>
		<tr><th>Num</th><th>Nom</th><th>Prénom</th><th>Dossard</th><th>distance</th><th>Club</th></tr>
	<?php for($i=0;$i<sizeof($tabRunners);$i++){
	$j=$i+1;
	?>
		
		<tr class="list">
			<td class="listing"><?php echo($j); ?></td>
			<td class="listing"><?php echo($tabRunners[$i]['nom']); ?></td>
			<td class="listing"><?php echo($tabRunners[$i]['prenom']); ?></td>
			<td class="listing" align="center"><?php echo($tabRunners[$i]['dossard']); ?></td>
			<td class="listing" align="center"><?php echo($tabRunners[$i]['distance']); ?></td>		
			<td class="listing" align="center"><?php echo($tabRunners[$i]['club']); ?></td>
		</tr>
		
	<?php } ?>
	</table>
<?php
	
	}

}

function listData($array,$separator){
	$temp="";
	for($i=0;$i<sizeof($array);$i++){
		$temp.=$array[$i].$separator;
	}
	return $temp;
}

function getKmH($diffTime,$km){

	$hour=substr($diffTime,0,2);
	$min=substr($diffTime,3,2);
	$sec=substr($diffTime,5,2);
	
	$minTotal=($hour*60)+$min;
	//min > total
	//1min >total/min
	//total/min*60
	
	return ($km/$minTotal)*60;
}

function  getMinOnKm($diffTime,$km){

	$hour=substr($diffTime,0,2);
	$min=substr($diffTime,3,2);
	$sec=substr($diffTime,5,2);
	
	$minTotal=($hour*60)+$min;
	
	return $minTotal/$km;

}

function getTimeDif($startTime,$endTime){
	$hourStart=substr($startTime,11,2);
	$minStart=substr($startTime,14,2);
	$secStart=substr($startTime,17,2);

	$monthStart=substr($startTime,5,2);
	$dayStart=substr($startTime,8,2);
	$yearStart=substr($startTime,0,4);

	//echo($startTime."=".$hourStart."-".$minStart."-".$secStart."-".$monthStart."-".$dayStart."-".$yearStart);
	
	$startStamp = mktime ($hourStart,$minStart,$secStart,$monthStart,$dayStart,$yearStart);

	
	
	
	$hourend=substr($endTime,11,2);
	$minend=substr($endTime,14,2);
	$secend=substr($endTime,17,2);

	$monthend=substr($endTime,5,2);
	$dayend=substr($endTime,8,2);
	$yearend=substr($endTime,0,4);

	$endStamp = mktime ($hourend,$minend,$secend,$monthend,$dayend,$yearend);

	$sec=$endStamp-$startStamp;
	
	$hour="00";
	$min="00";
	if($sec>=60){
		$min=floor($sec/60);
		$sec=$sec-($min*60);
		if($min>=60){
			$hour=floor($min/60);
			$min=$min-($hour*60);
			
		}	
	}
	
	if(strlen($min)<2){
		$min="0".$min;
	}
	
	if(strlen($hour)<2){
		$hour="0".$hour;
	}
	
	if(strlen($sec)<2){
		$sec="0".$sec;
	}
	return $hour.":".$min.":".$sec;
		
}

?>
